import {reactive, inject, computed, getCurrentInstance} from 'vue';
export function useLonglist(obj:any){
  const { proxy } :any = getCurrentInstance();
  let searchForm = reactive({
    keyword: null,
    current: 1,
    size: 20,
  });
  let listApi = ref(obj.listApi);
  let resKey = ref(obj.resKey);
  let longList = reactive({
    list:[],
    current: 0,
    size: 20,
    total: 0,
  });
  const getList = (type) =>{
    if(type == 'refresh'){  
      longList.current = 1;
      searchForm.current = 1;
      longList.list = [];
    }else{
      if(longList.total > longList.list.length){
        longList.current ++;          
        searchForm.current ++;          
      }else{
        uni.showToast({title:'没有更多了',icon: 'none'});
        return;
      }
    }
    let param = proxy.$util.getParam(searchForm);
    return new Promise((resolve, reject) => {
        proxy.$http('get', listApi.value, param ,(data,msg) => {
          if(resKey.value == 'data'){
            if(data){
              longList.list = longList.list.concat(data || []);
            }
          }else{
            if(data){
              longList.list = longList.list.concat(data.records || []);
              longList.total = data.total;
            }
          }
          resolve(data);
        }, msg => {
            reject(msg);
        });
    })    
  };
  return {
    searchForm,
    listApi,
    longList,
    getList,
  }
}